﻿using System;
using System.Windows.Forms;

namespace Skylabs.Breeze
{
    public partial class frmServer : Form
    {
        public frmServer()
        {
            InitializeComponent();
        }

        private void frmServer_Load(object sender, EventArgs e)
        {
            Server.Start();
            
        }

        private void frmServer_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                richTextBox1.Text = "";
                foreach (String s in Program.TraceListener.Events)
                    richTextBox1.Text += s;
                Program.TraceListener.OnEventAdd += new EventHandler<TraceCacheEvent>(TraceListener_OnEventAdd);                
            }
            else
            {
                Program.TraceListener.OnEventAdd -= TraceListener_OnEventAdd;
            }
        }

        void TraceListener_OnEventAdd(object sender, TraceCacheEvent e)
        {
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(new Action(() =>
                        {
                            write_event(e);                  
                        }));
                else
                    write_event(e);
            }
            catch (Exception ex)
            {
                if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();

            }
        }

        private void write_event(TraceCacheEvent e)
        {
            try
            {
                richTextBox1.Text += e.Event;
            }
            catch (Exception ex)
            {
                if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
            }
        }

        private void startServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Server.Start();
            
        }

        private void stopServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Server.Stop();
        }

        private void frmServer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!Program.Quitting)
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.Visible)
                this.Visible = false;
            else
            {
                this.Visible = true;
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            frmTransferFile tf = new frmTransferFile();
            tf.Show();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.Quitting = true;
            Skylabs.Breeze.Server.Stop();
            Application.Exit();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Program.SettingsWindow == null)
            {
                Program.SettingsWindow = new frmSettings();
                Program.SettingsWindow.ShowDialog(this);
            }
        }
    }
}
